/*
 * Copyright 2006-2007 BearingPoint GDC
 *  
 *           file :     CompressUtils.java
 *         project:     core
 *
 *     Create Date:     2007-1-9
 *          Author:     kevin.wang
 *
 *  [The change log]
 */

package com.mycompany.app.util.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * A Simple Compress File Utilities using java.util.zip
 * 
 * @author kevin.wang 2007-1-9
 */

public class CompressUtils {

    /**
     * Compress a group of files into a file
     * 
     * @param fileNames
     * @param outFileName
     */
    public static void compressFiles(String[] fileNames, String outFileName) {
        final Log log = LogFactory.getLog(CompressUtils.class);

    	// check input params. If arguments are invalid, return directly.
    	if( ArrayUtils.isEmpty( fileNames)) {
    		log.error( "Argument fileNames must not be null or empty.");
    		return;
    	}
    	
    	if( StringUtils.isBlank( outFileName )) {
    		log.error( "Argument outFileName must not be null, empty or blank!");
    		return;
    	}
    	
    	// check the outputFile. if it exists, print warning message.
    	File outputFile = new File( outFileName);
    	if( outputFile.exists()) {
    		log.warn( "File of outFileName, " + outFileName + " exists, system will overwrite it.");
    	}

    		
        // Create a buffer for reading the files
        byte[] buf = new byte[1024 * 4];
        ZipOutputStream out = null;

        try {
            // Create the ZIP file
        	// obsoleted by martin.liu, 2007/8/3.
            // ZipOutputStream out = new ZipOutputStream(new FileOutputStream( outFileName, false));
        	out = new ZipOutputStream( new FileOutputStream( outputFile));

            // Compress the files
            for (int i = 0; i < fileNames.length; i++) {
                FileInputStream in = new FileInputStream(fileNames[i]);
                // Add ZIP entry to output stream.
                out.putNextEntry(new ZipEntry(
                        FilenameUtils.getName(fileNames[i])));
                // Transfer bytes from the file to the ZIP file
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                // Complete the entry
                out.closeEntry();
                in.close();
            }
        } catch (IOException e) {
            log.error("Compress File Error :" + e.getMessage());
            e.printStackTrace();
        } finally {
            // Complete the ZIP file
        	if( out != null)
				try { out.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
        }
    }

    /**
     * Compress a file
     * 
     * @param fileName
     * @param outFileName
     */
    public static void compressFile(String fileName, String outFileName) {

        compressFiles(new String[] { fileName }, outFileName);
    }
}
